const SitesModel = require('@/models/sites.model')
const JsonResult = require('@/utils/httpResponse.unit')
const { getSessionuserId } = require('@/utils/common.util')
const SiteFolderModel = require('@/models/site_folder.model')
const uploadIconStream = require('@/utils/getFavicon')
const { apiGetToolTree, apiFuzzySearchByName } = require('@/http')

class SitesControllers {
  /**
   * find sites and judge permission
   * @param {*} req
   * @param {*} response
   */
  async findUserSites (req, response) {
    try {
      const folderId = req.query.folderId
      const sessionuserId = await getSessionuserId(req)
      const folderItem = await SiteFolderModel.findOne({ id: folderId })
      // 如果当前收藏夹私密则不返回
      if (
        !folderItem ||
        (folderItem.isPrivate === '1' &&
        sessionuserId !== folderItem.userId)) {
        return JsonResult.fail({ req, response, message: '收藏夹不存在' })
      }
      const data = await SitesModel.find(folderId)
      JsonResult.success({
        req,
        data,
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  /**
   * find all sites
   * @param {*} req
   * @param {*} response
   */
  async findToolTreeList (req, response) {
    try {
      const sessionId = await getSessionuserId(req)
      const data = await apiGetToolTree(sessionId)
      JsonResult.success({
        req,
        data,
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  async findToolByName (req, response) {
    try {
      const data = await apiFuzzySearchByName(req.query.name)
      JsonResult.success({
        req,
        data,
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  /**
   * user add site
   * @param {*} req
   * @param {*} response
   */
  async additionSite (req, response) {
    try {
      const { folderId, name, url, siteDesc } = req.body
      const sessionuserId = await getSessionuserId(req)
      const site = await SiteFolderModel.findOne({ userId: sessionuserId, id: folderId })
      if (!site) {
        return JsonResult.fail({ req, response, message: '文件夹不存在' })
      }
      await SitesModel.add({ folderId, name, url, siteDesc, userId: sessionuserId })
      JsonResult.success({
        req,
        response,
        message: '新增成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '新增失败' })
    }
  }

  /**
   * user delete site by siteId
   * @param {*} req
   * @param {*} response
   * @returns
   */
  async deleteSite (req, response) {
    try {
      const id = req.query.id
      const sessionuserId = await getSessionuserId(req)
      const site = await SitesModel.findOne({ id })
      if (!site) {
        return JsonResult.fail({ req, response, message: '站点不存在' })
      }
      if (site.userId !== sessionuserId) {
        return JsonResult.httpStatus(req, response, 403, {
          message: 'No permission to delete site',
          code: '9999'
        })
      }
      await SitesModel.delete({ id })
      JsonResult.success({
        req,
        response,
        message: '删除成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '删除失败' })
    }
  }

  /**
   * user update site
   * @param {*} req
   * @param {*} response
   */
  async updateSite (req, response) {
    try {
      const { id, name, url, siteDesc } = req.body
      const sessionuserId = await getSessionuserId(req)
      const site = await SitesModel.findOne({ id })
      if (!site) {
        return JsonResult.fail({ req, response, message: '站点不存在' })
      }
      if (site.userId !== sessionuserId) {
        return JsonResult.httpStatus(req, response, 403, {
          message: 'No permission to update site',
          code: '9999'
        })
      }
      await SitesModel.update({ id, name, url, siteDesc })
      JsonResult.success({
        req,
        response,
        message: '更新成功'
      })
      const icon = await uploadIconStream(url)
      SitesModel.update({ id, icon })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '更新失败' })
    }
  }
}

module.exports = new SitesControllers()
